package com.ruoyi.standingbook.v2toilet.logic;

import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.standingbook.v2toilet.enums.ItemStatusEnum;
import com.ruoyi.standingbook.v2toilet.service.IToiletBillService;
import com.ruoyi.stat.toilet.logic.HttpLogic;
import com.ruoyi.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Slf4j
public class ConWorker extends Thread {
    int workTime;
    CountDownLatch latch;
    ISysConfigService configService;
    IToiletBillService iToiletBillService;
    String jsonData;
    private List<String> flowCodes;

    public ConWorker(int workTime, CountDownLatch latch, IToiletBillService billService, ISysConfigService configService, String data, List<String> flowCodes) {
        this.workTime = workTime;
        this.latch = latch;
        this.configService = configService;
        this.jsonData = data;
        this.iToiletBillService = billService;
        this.flowCodes = flowCodes;
    }

    public void run() {
        doWork();//工作了
        latch.countDown();//工人完成工作，计数器减一

    }

    private void doWork() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", IdUtil.fastSimpleUUID());


        int code = -1;
        try {
            String response = HttpLogic.postBody(configService, "/workspace/elec/inspection/ticketsImport", jsonData);
            JSONObject jsonObject = JSONUtil.parseObj(response);
            code = jsonObject.get("code", Integer.class, true);
            String resp = UnicodeUtil.toString(response);
            System.out.println(resp);
            log.info("--> to zijian -- {}", jsonData);
            map.put("body", IdUtil.fastSimpleUUID());
            map.put("createTime", new Date());
            map.put("resp", resp);
            map.put("flowCodes", String.join(",", flowCodes));
            if (code == 0) {
//                iToiletBillService.updateByFlowCode(ItemStatusEnum.COMMIT, flowCodes);
            }
            Thread.sleep(workTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
